<!-- pages/login/components/mobile.vue -->
 
<script lang>
  export default {
    options: {
      styleIsolation: 'shared',
    },
  }
</script>

<script setup>
import { ref } from 'vue'
import {getCodeApi,loginByMobileApi} from '@/apis/user.ts'
import { userStore } from '@/stores/user'

// 1. 倒计时处理
  const showCountDown = ref(false)
  // 提交表单数据
  const formRef = ref(null)
   // 表单数据
  const formData = ref({
    mobile: '13230000011',
    code: '',
  })
  const form =ref()
  const isAgree = ref(false)

  const getMobileCode = async () => {
    try {
      if (!formRef.value) return
      const res= await formRef.value.validateField(['mobile'])
      const codeRes= await getCodeApi({
        mobile: formData.value.mobile,
        type: 'login',
      })
      formData.value.code = codeRes.code
      // 1.0 判断手机号不能为空
      // 1.1 显示倒计时
      showCountDown.value = true
    } catch (error) {
      console.log(error)
    }
  }
  const onFormSubmit = async () => {
    try {
      if (!formRef.value) return
      // 验证通过后会返回表单的数据
      const formData = await formRef.value.validate();
      if (!isAgree.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
      const res =await loginByMobileApi(formData);
      console.log(res)
      // 持久化存储 token
      userStore.token = res.token
      uni.showToast({ title: '登录成功', icon: 'success' })
      // 临时跳转到首页面
      uni.switchTab({
       url: '/pages/index/index',
      })
    } catch (error) {
      console.log(error)
    }
  }


  // 验证表单数据的规则
  const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
      ],
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' },
      ],
    },
  }

  
</script>

<template>
  <uni-forms class="login-form" :model="formData" :rules="formRules" ref="formRef">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        maxlength="11"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        v-model="formData.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown :show-day="false" :show-hour="false" :show-minute="false" :second="59"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio v-model="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onFormSubmit">登 录</button>
  </uni-forms>
</template>


<style lang="scss">
  @import './styles.scss';
</style>